import { join } from 'path'

import webpack from 'webpack'
import nodeExternals from 'webpack-node-externals'

const { rootDir } = require('./consts')

export function getNodeConfig(mode: NonNullable<webpack.Configuration['mode']>) {
  return {
    mode,
    devtool: 'source-map',
    resolve: { mainFields: ['esnext', 'module', 'main'] },
    optimization: {
      minimize: false,
      splitChunks: false,
      runtimeChunk: false,
      nodeEnv: false,
    },
    target: 'node',
    node: false,
    externals: nodeExternals({
      modulesDir: join(rootDir, 'node_modules'),
      allowlist: [/@maiev\/(.*)/, /@perfkit\/(.*)/],
    }),
    output: {
      path: join(rootDir, 'output'),
      filename: '[name].js',
      chunkFilename: '[name].js',
      libraryTarget: 'commonjs2',
    },
    module: {
      parser: {
        javascript: {
          commonjsMagicComments: true,
        },
      },
    },
  } as webpack.Configuration
}
